package com.kgo.log.logtools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kgo.log.logtools.config.SystemConfig;
import com.kgo.log.logtools.params.SysParams;
import com.kgo.log.logtools.tools.PathTools;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author keepgoon
 * @Description: TODO
 * @date 2019/10/1620:31
 */
@Component
public class IndexClass {
    private Logger log = LoggerFactory.getLogger(IndexClass.class);
    @Autowired
    private PathTools pathTools;
    @Autowired
    private SystemConfig systemConfig;
    @PostConstruct
    private void startChrome(){
        initSysParams();
        getSysConfig();
    }
    private void initSysParams(){
        SysParams.basePath = StringUtils.isNotBlank(pathTools.getJarPath())?pathTools.getJarPath() : null;
    }
    private void getSysConfig(){
        if(StringUtils.isBlank(SysParams.basePath)) return;
        boolean fileIsExist = FileUtil.exist(SysParams.basePath+SysParams.configFile);
        log.debug("配置文件是否存在 {}  ",fileIsExist);
        if(fileIsExist){
            FileReader fileReader = new FileReader(SysParams.basePath+SysParams.configFile);
            String configStr = fileReader.readString();
            JSONObject json = JSON.parseObject(configStr);
            systemConfig.init(json);
        }
    }


}
